/*
* 文 件 名:  DepartmentDAOImpl.java
* 版    权:  Tydic Technologies Co., Ltd. Copyright 1993-2012,  All rights reserved
* 描    述:  部门操作接口实现
* 修 改 人:  aganzhoul
* 修改时间:  2012-9-14
* 跟踪单号:  <跟踪单号>
* 修改单号:  <修改单号>
* 修改内容:  <修改内容>
*/

package com.tydic.simple.dao.user.impl;

import java.sql.SQLException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;

import com.tydic.framework.dao.AbstractIbatisDAO;
import com.tydic.framework.dataobject.ResultDTO;
import com.tydic.framework.exception.DAOException;
import com.tydic.simple.dao.user.IDepartmentDAO;
import com.tydic.simple.dto.user.DepDTO;
import com.tydic.simple.model.user.Department;

/**
* 部门操作接口实现
* 
* @author  aganzhoul
* @version  [1.0, 2012-9-14]
* @since  [权限管理/1.0]
*/

public class DepartmentDAOImpl extends AbstractIbatisDAO implements IDepartmentDAO {
    
    private static final Logger LOGGER = LoggerFactory.getLogger(DepartmentDAOImpl.class);
    
    /** {@inheritDoc} */
    @Override
    public Long saveDepartment(Department department)
        throws DAOException {
        try {
            sqlMapClient.insert("user.INSERT_DEPARTMENT", department);
        }
        catch (SQLException e) {
            LOGGER.error("saveDepartment exception", e);
            throw new DAOException("saveDepartment exception", e);
        }
        return department.getDepId();
    }
    
    /** {@inheritDoc} */
    @Override
    public int deleteDepByCode(String depCode)
        throws DAOException {
        try {
            return sqlMapClient.delete("user.DELETE_DEP_BY_CODE", depCode);
        }
        catch (SQLException e) {
            LOGGER.error("deleteDepByCode exception", e);
            throw new DAOException("deleteDepByCode exception", e);
        }
    }
    
    /** {@inheritDoc} */
    @Override
    public Department findDepByCode(String depCode)
        throws DAOException {
        try {
            return (Department)sqlMapClient.queryForObject("user.FIND_DEP_BY_CODE", depCode);
        }
        catch (SQLException e) {
            LOGGER.error("findDepByCode exception", e);
            throw new DAOException("findDepByCode exception", e);
        }
    }
    
    /** {@inheritDoc} */
    @SuppressWarnings("unchecked")
    @Override
    public ResultDTO<Department> findDepartment(DepDTO depDTO)
        throws DAOException {
        ResultDTO<Department> result = new ResultDTO<Department>();
        try {
            result.setResult(sqlMapClient.queryForList("user.FIND_DEPARTMENT", depDTO));
            BeanUtils.copyProperties(depDTO.getPagination(), result.getPagination());
            result.getPagination().setTotal((Integer)sqlMapClient.queryForObject("user.FIND_DEPARTMENT_COUNT", depDTO));
        }
        catch (SQLException e) {
            LOGGER.error("findUser exception", e);
            throw new DAOException("findUser exception", e);
        }
        return result;
    }
    
}
